FreiBox: A Versatile Open-Source Behavioral Setup for Investigating the Neuronal Correlates of Behavioral Flexibility via 1-Photon Imaging in Freely Moving Mice

Abstract To survive in a complex and changing environment, animals must adapt their behavior. This ability is called behavioral flexibility and is classically evaluated by a reversal learning paradigm. During such a paradigm, the animals adapt their behavior according to a change of the reward contingencies. To study these complex cognitive functions (from outcome evaluation to motor adaptation), we developed a versatile, low-cost, open-source platform, allowing us to investigate the neuronal correlates of behavioral flexibility with 1-photon calcium imaging. This platform consists of FreiBox, a novel low-cost Arduino behavioral setup, as well as further open-source tools, which we developed and integrated into our framework. FreiBox is controlled by a custom Python interface and integrates a new licking sensor (strain gauge lickometer) for controlling spatial licking behavioral tasks. In addition to allowing both discriminative and serial reversal learning, the Arduino can track mouse licking behavior in real time to control task events in a submillisecond timescale. To complete our setup, we also developed and validated an affordable commutator, which is crucial for recording calcium imaging with the Miniscope V4 in freely moving mice. Further, we demonstrated that FreiBox can be associated with 1-photon imaging and other open-source initiatives (e.g., Open Ephys) to form a versatile platform for exploring the neuronal substrates of licking-based behavioral flexibility in mice. The combination of the FreiBox behavioral setup and our low-cost commutator represents a highly competitive and complementary addition to the recently emerging battery of open-source initiatives.


Introduction
Behavioral flexibility is essential to live in a complex and changing environment and to adapt to the infinite contexts of our daily life. An action that was appropriate in the past can become obsolete after a contextual change. This cognitive ability requires multiple mental processes which are supported by a vast cortico-subcortical network (Schoenbaum et al., 2009;Floresco and Jentsch, 2011;Izquierdo et al., 2017). By using behavioral flexibility paradigms, it is possible to explore cortical, basal ganglia, or thalamic network activities, as well as to cover many fields in behavioral neuroscience (e.g., sensory discrimination, decision-making). To do so in an animal model, we need to measure behavioral responses that can be attributed to association, contingency learning, decision, or adaptation.
Typical behavioral responses in freely moving animals are nose pokes (NPs) in specific chambers, visits in maze arms, presses on a specific lever, or movements of a joystick in a specific direction to obtain a reward (Castañé et al., 2010;Parker et al., 2016;Izquierdo et al., 2017;Belsey et al., 2020;Lee et al., 2020;Mazziotti et al., 2020;Vassilev et al., 2022). For instance, Vassiliev et al. (2022) recently developed a well documented and open-source setup to perform a Go/No-Go task. While this approach is already very useful to explore behavioral flexibility in freely moving mice, no currently available setup permits measurement of directional licking, a behavioral paradigm that can access numerous cognitive processes in head-restrained mice (Mayrhofer et al., 2019;Wu et al., 2020;Bollu et al., 2021;Catanese and Jaeger, 2021;Wang et al., 2021). Such a tool would provide the opportunity to perform translational studies between head-fixed and freely moving conditions. Focusing on licking behavior, we have designed a fully open-source and cost-efficient (;900 e, Table 1) behavioral platform to explore behavioral flexibility in freely moving mice. This new box, called FreiBox ( Fig. 1), is capable of monitoring a wide variety of licking-based tasks, such as discriminative learning (DL) and serial reversal learning (RL), by tracking online licks with strain gauge (SG)-based force lickometers.
To track licking behavior online, conventional approaches use capacitive or optical methods to isolate individual licks. However, these methods generate electrical noise or are difficult to implement in freely moving mice. To address these issues, we developed a unique noiseless and sensitive mouse force lickometer ideally suited for freely moving conditions. To facilitate the integration of this new device into our platform, we added an adjustable online threshold detection system, making it compatible with nearly any behavioral platform, including those for head-fixed preparations. In addition, we designed a new licking chamber (LC) equipped with a gate, to control the accessibility of the licking spout, and a fiber optics-based infrared (IR) beam system with a very short sensing distance (2 mm) to facilitate its application to detect NP with implanted animals. We showed that by combining FreiBox with other open-source technologies, we are able to perform 1-photon calcium imaging in freely moving mice for a competitive cost per unit (,9000 e, Table 1). To this end, we have also included a low-cost (,150 e, Table 1) commutator that is equipped with a novel sensing system that improves online tracking of cable rotations without impairing the 1-photon calcium imaging via a Miniscope. Finally, to support the open-source philosophy, we built FreiBox by assembling multiple modules that are shared and fully available on the Github site of our laboratory (https://github.com/Optophys-Lab/FreiBox).

FreiBox platform and its operant instruments
Influenced by the lever-press tasks in which an animal chooses between pressing on the right or left lever (Boulougouris et al., 2007;Brady and Floresco, 2015), FreiBox (Fig. 1C,D) offers the mice directional licking. We equipped FreiBox with a NP and a LC placed at opposing sides of the Plexiglas box (40 Â 20 Â 15 cm; Fig.  1C-H). The LC contains two SG lickometers and a chamber gate ( Fig. 1E-G). The LC gate provides control over the licking access, similar to the lever retraction in commercial boxes. Inspired by previous optical lickometers (Schoenbaum et al., 2001;Isett et al., 2018), we included an optic fiber IR detection system at 2 mm from the entrance of both chambers to detect head entries (Fig. 1G,H). This design offers the advantage of decreasing the detection distance from the entrance and reducing the risk for the animal to touch the box walls with its implants. In addition to the operant instruments, we added several components (Fig. 1C,D) that can be used to deliver water or to generate light or auditory cues. An UltiMaker 3D printer equipped with black PLA filament was used to make the 3D-printed parts.
To control FreiBox and its electronic modules, we used the programmable microcontroller Arduino Mega 2560 (Arduino; Fig. 1I). This board can be easily interfaced with a Python library (Pyserial) to control behavioral tasks, set the task parameters, or collect results to perform online plotting. Although this Arduino board has the major advantage of interacting directly with sensors or other electronic modules (e.g., sound card) through several analog and digital input/output (I/O) pins and multiple libraries (Fig. 1I), it is a "one core" device that can execute only one instruction at a time. This dramatically reduces the processing speed. According to the manufacturer (https://www.arduino.cc/reference/en/language/functions/ analog-io/analogread/), the analog sampling rate is relatively slow (,10 kHz for a single channel) and decreases with the number of recorded channels. This contrasts strongly with the high-speed capability of an Arduino to read and write digital signals with the Arduino library "DIO2" (;4 ms, 250 kHz; https://www.arduino.cc/reference/ en/libraries/dio2/). The input/output reactivity of the Arduino Mega 2560 (Extended Data Fig. 1-1) has a delay of ,20 ms. Based on those results, we built electrical modules which interact with the Arduino board by using digital signals only (Fig. 1I). This transistortransistor logic (TTL)-based modular organization facilitates the electronic integration and transposition into different behavioral controller platforms (Raspberry Pi or Teensy). Hence, we developed several hardware modules that can be incorporated or removed depending on the task needs. For instance, since the Arduino Mega board has half of its digital pins available in FreiBox, it is possible to include several other spouts to perform sequence learning, without comprising the reading speed of more than several tens of microseconds per additional spout. We provide here the circuits used to build FreiBox (Extended Data Fig. 1-2).

Sensitivity evaluation of the strain gauge lickometer
When a mouse licks on the spout of the SG lickometer ( Fig. 2A,B), the amplified SG signal (SGs; Extended Data  Fig. 2-1A) that can be easily recorded by any analog-todigital converter (e.g., Open Ephys) and analyzed offline (see the following section) to extract individual licks (Fig. 2C, SG-offline detection). Given the large signalto-noise ratio of the SGs, we connected a voltage comparator circuit (Extended Data Fig. 1-2C) to track the licking behavior online (Fig. 2C, SG-online TTL). Such a circuit generates a TTL pulse when the lick peaks of the SGs cross a defined reference voltage threshold. For the validation experiment described below, this threshold was set at 100 mV above the baseline and kept constant for all mice.
The term touch sensor (TS) refers to all methods and electronic circuits derived from the "Electronic Drinkometer" published by Hill and Stellar (1951). This device had been designed to detect a voltage or capacitive change when a rodent tongue touches the liquid; in other words, the animal becomes part of an electronic circuit (Schoenbaum et al., 2001) and "completes" it during licking (Hill and Stellar, 1951). To test whether the sensitivity of the SG lickometer ( Fig. 2) is comparable to such classic TS, we placed six water-restricted mice in a water-delivery chamber (20 Â 20 Â 20 cm), containing a single licking spout attached to an SG ( Fig. 2A) and connected to the TS circuit (Goltstein et al., 2018). The box floor was covered with a metal plate connected to the ground of the TS circuit. High-speed video recordings (1000 frames/s; Area Scan Camera, model ace acA1300-200uc, Basler) were simultaneously performed ( Fig. 2B) to control the contact of the tongue of the mouse with the spout and measure the "ground truth licks" (GTlicks). The videos were then annotated offline with the software BORIS (Behavioral Observation Research Interactive Software) to extract the time stamps of the licks. To align the licking detection time stamps, the TTL from the camera (indicating the exposure window of each video frame) were recorded with an Open Ephys system (sampling rate, 30 kHz), simultaneously with the SGs, SG-online TTL, and TTL of the TS. The GT-licks were then aligned to the SGs (analog and SG-online TTL) and the TS TTL, to count true-positive (TP) and false-positive (FP) licks (Fig. 2C). The sensitivities (Sensitivity = [TP/(TP 1 FN)]; where FN is false-negative) and positive predictive values (PPVs; PPV = [TP/ (TP 1 FP)]) were evaluated to compare the detectors. The PPV was quantified to compare the false detection rate occurring with both lickometers. For the TS, FP licks occurred following an electrical artifact or when the mouse was in close contact or in immediate proximity to the spout or water droplet. For the SG, false detection occurred only when the SG signal crosses the reference voltage threshold of the comparator circuit, thus mainly when the mouse exerts a force pressure on the spout.
To extract individual licks, the SGs has been first downsampled (with an antialiasing low-pass filter) to 1 kHz (MATLAB function "resample") and low-pass filtered at 64 Hz. The function "findpeak" (MATLAB) was then used to extract the peaks of individual licks. To improve the detection, we used the following parameters: a minimal peak distance of 60 ms, a minimal prominence of 0.015, and a minimal peak width of 50 ms. We applied a matching procedure to validate each detected lick, by comparing the delay of each detected lick with its closer GT value. A lick was considered as TP if this delay was less than the minimal interlick interval used for the SGoffline detection (60 ms).

Animals and water restriction protocol
All animal procedures were performed in accordance with the guideline RL 2010/63/EU and approved by the Regierungspräsidium Freiburg. The animals were housed (Blueline type 1284 L, Tecniplast) with a humidity between 45% and 65%, and a temperature between 20°C and 24°C, under a 12 h light/dark cycle (light period from  Commutator  111  3  Tools  160  4 Miniscope UCLA V4 1580 5 Open Ephys system 3479 6 Laser controller 900 7 Computer 1500 Total 8633 A precise list of the components and providers is available at https://github. com/Optophys-Lab/FreiBox. The prices were updated in June 2022. Figure 1. FreiBox platform and its operant instruments. A, The FreiBox platform combines licking-based behavior with 1-photon calcium imaging, electrophysiological recordings, and optogenetic manipulation. B, FreiBox is connected to a workstation equipped with a Python interface to set the task parameters and perform online analyses of the behavioral results. To perform optogenetic experiments, FreiBox can control a laser directly with an Arduino (Laser shutter) or indirectly via a PulsePal device. An Open Ephys acquisition system integrated into the platform performs electrophysiological recordings and can be used to synchronize behavioral events (such as the box synchronization and the licking signal) and the frame signal of the Miniscope UCLA V4 register. An open-source and cost-efficient commutator has also been developed to perform calcium imaging (see Extended Data Fig. 4-1). See Table 1 for the cost estimation of the FreiBox platform. C, Pictures of the different components assembled to build FreiBox. Component numbers: 1 and 2, house light; 3, speaker; 4, NP chamber; 5, LC; 6, LC Gate; 7, SG lickometer. D-G, Picture of the licking chamber (D) equipped with a gate, two lickometers (E), and an HE detection module, based on the breaking of an IR beam (F) relayed by two fiber optics placed at 2 mm from the entrance (G). H, Input/output TTL signal mapping received and sent by the Arduino to react and control the electronic modules. In response to an HE in the NP/LC or licking on the spouts, the Arduino sends a TTL pulse to deliver water, turn on and off the house lights, open the LC gate, generate an auditory cue, generate a synchronization signal, and control other devices (e.g., a laser shutter). See Extended Data Figure 1-1 for the evaluation of the input/output reactivity of FreiBox and Extended Data Figure 1-2 for the electronic circuits of the FreiBox modules.
To motivate the mice to perform the behavioral task, they were maintained under water restriction up to 5 d of the week followed by 1-2 d of water ad libitum. During the water restriction period, a careful monitoring of the weights of mice was performed on a daily basis. Care was taken to ensure that they did not weigh ,80% of their free-feeding weight measured at the start of the week of deprivation. For training days, the animals consumed at least 1 ml of water. When weight fell to ,80%, additional water was given until they recovered (.80% of the initial weight). In the periods without behavioral assays, the mice received water ad libitum.

Behavioral task
The RL task is a freely moving directional licking paradigm ( Fig. 3A-D) adapted from a head-restraint condition (Mayrhofer et al., 2019). During the DL phase, the mice had to explore to find the reward spout by licking on one of the two available spouts (Fig. 3A). After the learning phase, the reward spout is automatically changed and the mice need to adapt their behavior (Fig. 3A, RL phase). To Figure 2. Validation of the strain gauge lickometer. A, Picture of the SG lickometer, composed by a 3D-printed holder attaching an SG connected to a licking spout. B, Picture taken by the high-speed camera used to define the ground truth licks and compare the sensitivity of the SG and touching lickometers. The metal plate on the box floor was used to ground the mice when the animal tongue touches the metallic spout. C, 5 s example of TP, FN, and FP licks from the 3 licking detection methods (SG-offline, SG-online, and touch sensor), defined by comparison with the ground truth licks defined by a high-speed video recording. On the top trace (calibration: 5 s, 100 mV; inset calibration: 200 ms, 50 mV), the online detection threshold used to induce the SG-online TTL is plotted. D, Distribution histogram of the interlick interval of the video GT and true-positive licks recorded with the 3 licking methods shown in C. (E). Box-and-whisker plots comparing the sensitivity of touching sensor and SG lickometers. One-way repeated-measures ANOVA (F = 4.906, p = 0.033) followed by Tukey's test (q = 34.3, *p = 0.031). See Extended Data Figure 2-1 describing the threshold method used to improve and monitoring the online lick detection.
Open Source Tools and Methods prevent to reverse the contingencies while they were not properly learned, the RL phase occurred only if the mouse reached (1) a minimum number of 15 trials (block length), and (2) an online performance criterion (70% hit in a sliding average window of 15 trials). To initiate a trial and to open the LC gate, the mice must perform a NP (Fig. 3B). They can then walk [walking delay (WD)] to enter their head into the LC [head entry (HE)] and lick on the correct spout during the licking response (LR) period (2 s). At the end of the LR, an auditory cue is played, consisting of a pure 5 kHz tone for the correct licking trials (or hit trials) or white noise for missed trials (entry in the LC without licking on a spout during the 2 s LR) or error trials (licking on the incorrect spout). After an incorrect or missed trial, the gate is automatically closed. In contrast, a reward anticipation (RA) period is maintained 1 s before the water delivery for the hit trials, to allow recording of reward-related activity prediction. In this configuration, the auditory cues become predictive of the outcomes. Finally, the LC gate is closed and the trial is finished after a 4 s postreward interval, allowing reward consumption.

Behavioral training
To train each mouse to perform the task, we slowly introduced each task component and personalized the training to respect the individual learning rate. The first stage consisted of the association among the licking spouts, the water reward, and the auditory cues. To accomplish this, the LC gate was left opened until the mouse placed its head in the LC. If the mouse licked on the spout, the pure tone cue (5 kHz, 85 dB) was played immediately, the reward was delivered, and the gate closed after 4 s. In contrast, if the mouse did not lick on the spout 2 s after the HE (missed trial), the white noise cue (85 dB) was played and the gate was closed. At this stage, until stage 3, only one spout was presented in the LC and a session for each licking spout was programmed to avoid a spatial bias. The second stage was similar, except that the licking response and anticipatory response were introduced. In the third stage, the NP was introduced, meaning that the LC gate was kept closed until the mouse performed a NP. During the fourth stage, we introduced the DL by presenting both spouts in the LC. To help the animal, we used immediate feedback by playing the white noise cue immediately after the mouse licked on the wrong spout, followed by LC gate closing. In the next training stage, the final paradigm was used to perform intersession reversal of DL session. In such configuration, the reward spout learned during DL in the morning is reversed for the afternoon sessions. Finally, the intrasession reversal task was used. At this final stage, only one session per day was used to maintain a high motivation level in the animals. With this protocol, the mice learned the task in a few weeks. For instance, the training of the cohort used for data provided in Figure 3 lasted on average 30 6 2 sessions (n = 8 mice), which corresponds to ;3 weeks when performing two sessions per day on 5 training days per week.

Surgery
All animal procedures were performed in accordance with the guideline RL 2010/63/EU and approved by the Regierungspräsidium Freiburg. General anesthesia was administered using a mixture of oxygen and isoflurane (induction, 3-5%; maintenance, 1.5-3%; CP Pharma), associated with a subcutaneous buprenorphine injection (0.05 mg/kg; Temgesic). The mice were then fixed on a stereotaxic frame (model 942, Kopf) and placed on heating blanket (Rodent Warmer X1, Stoelting). During surgery, the depth of anesthesia was determined by the extinction of the pain reflexes (intertoe and eyelid closure reflexes), and the eyes were protected from dehydration with eye ointment (Bepanthen; catalog #798-037, Henry Schein Medical). Five minutes before the rostrocaudal skin incision, a local surface lidocaine anesthesia (xylocaine gel, 2%; catalog #1138060, Shop-Apotheke) was For 1-photon calcium imaging in C57BL/6J mice, a virus injection procedure (De La Crompe et al., 2020) was performed right before gradient-index (GRIN) lens implantation. After craniotomy, a virus solution (rAAV1-hSyn-jGCamP7f; catalog #104488-AAV1, Addgene) was injected under stereotaxic conditions (400 nl, 3 Â 10 12 viral genomes/ml) into OFC [anteroposterior (AP), 12.5 mm from bregma; mediolateral (ML), 11.5 mm from midline; dorsoventral (DV), À1.8 mm from cortical surface] using glass microcapillary (tip diameter, approximately 35 mm; 1-5 ml Hirschmann microcapillary pipette; catalog #Z611239, Sigma-Aldrich) connected to a custom Openspritzer pressure system (Forman et al., 2017). To perform GRIN lens implantation, we followed the instructions provided by the Miniscope V4 group (http:// miniscope.org/index.php/Online_Workshop). After craniotomy and a tunneling procedure using 25 gauge needle, the GRIN lens (diameter, 0.5 mm; pitch, 2; catalog #1050-002183, Inscopix), preassembled to the baseplate, was inserted above the OFC (AP, 12.3 mm from bregma; ML, 11.5 mm from midline; DV, À1.8 mm from cortical surface). The lens was then secured by using a light-blocking cement obtained by mixing a Open Source Tools and Methods small amount of a black pigment (black iron oxide; catalog #832036, Kunstpark) with a dental cement (Paladur, Kulzer). To build the mounted GRIN lens and adjust the working distance, we used a custom Miniscope holder (https://github.com/Optophys-Lab/FreiBox) in combination with a lens holder available on the github site for Moorman's laboratory (https://github.com/moormanlab/ miniscope-goodies) before cementing the GRIN lens to the baseplate. At the end of surgery, a cap was secured to the baseplate to protect the lens. All Miniscope materials were purchased at the Open Ephys store or were 3D printed with a Ultimaker 3D printer and black PLA filament.
To prevent postoperative pain, we performed subcutaneous injections of buprenorphine (0.05 mg/kg every 6 h; Temgesic) and carprofen (5 mg/kg every 24 h; Rimadyl, Zoetis) during the next 3 d postsurgery. To continue the analgesic treatment during the night, we provided a buprenorphine (Temgesic) solution (0.1 g/L) mixed in drinking water containing 5% (w/v) D-glucose (100 ml; catalog #G8769, Sigma-Aldrich) to mask the drug taste. The weight and the general condition of the animals were monitored daily in the 4 d following surgery and weekly after the recovery of the animal.

Miniscope recording and data analysis
Before starting the Miniscope recordings, trained animals were habituated to hold a dummy 3D-printed Miniscope during two to three sessions (https://github. com/Aharoni-Lab/Miniscope-V4). Once habituated, the mice were placed in the behavioral box with the Miniscope secured to the implanted baseplate. During the behavioral sessions, Open Ephys recordings (sampling rate, 30 kHz) were performed simultaneously with calcium imaging to synchronize, with the same clock system, the data streams coming from the FreiBox (Fig. 4C, synchronization signal) and the Miniscope V4 (Fig. 4C, Miniscope frames). After the acquisition (LIFEBOOK U749 laptop, Fujitsu), the videos were preprocessed to remove the noise produced by the electrowetting lens driver (https://github.com/Aharoni-Lab/Miniscope-V4/wiki/Removing-Horizontal-Noise-from-Recordings) and cropped to preserve the part of the field of view containing only the GRIN lens. We then used the opensource tool library CaImAn (gSig/gSiz: 3/13; Giovannucci et al., 2019) to extract the spatial footprints and their associated temporal calcium traces, expressed as denoised temporal traces (parameter C of the source extraction algorithm CNMF-E). All the video-processing steps were made with Python, and the following analyses were performed in MATLAB. To find behavioral correlations of the calcium traces, we first aligned all behavioral events to the closest recorded frames time stamp. We then calculated the waveform average of the median absolute deviation (MAD) of the denoised temporal traces. Based on this approach, we defined units as "modulated" when their averaged calcium peaks crossed a 3Â MAD threshold. Finally, we classified modulated footprints as initiation, licking, or postlick neurons if their averaged MAD peaks occurred during the WD, the licking response, or RA period, respectively.

Transcardiac perfusion and histologic control
After completion of the experiments, the mice received an overdose of a pentobarbital sodium (1 ml; 1.6 g/L, i.p.; Narcoren). After respiratory arrest, a transcardiac perfusion (PBS 0.01 mM followed by PBS 0.01 mM/formaldehyde 4%) was performed for histologic validations. The brain was kept overnight in a solution of PBS 0.01 mM (catalog #70011-051, Thermo Fisher Scientific)/formaldehyde 4% (v/v; catalog #E15711, Science Services), cryoprotected several days in a solution of PBS 0.01 mM/sucrose 30% (w/v; catalog #1076511000, Merck Millipore), and then cut into 50 mm slices with a sliding microtome (catalog #SM2010 R, Leica Biosystems). The images were acquired with an Axioplan 2 microscope (Zeiss) controlled by the software Axiovision (version 4.8, Zeiss). Shading correction and stitching were performed with Fiji (based on ImageJ from National Institutes of Health) using the BaSiC (Peng et al., 2017) and Grid/Collection plugins, respectively.

Figure design and statistical analysis
All plots were generated with MATLAB and Python and were assembled with Illustrator CS6 (Adobe) to generate the figures. For Figure 3A and Extended Data Figure 3-1D, the mouse head drawing was downloaded from https://scidraw.io/. The statistical analyses were performed (De La Crompe et al., 2020) using SigmaPlot 12 (Systat Software). For independent samples, we applied the normality (Shapiro-Wilk test) and equal variance tests. A t test was used to compare samples if they were normally distributed and their group variances equal. Otherwise, the Mann-Whitney signed-rank test was performed. For dependent samples, the paired t test was used for normally distributed paired samples. In contrast, when the normality distribution test failed (Shapiro-Wilk test, p , 0.05), the Wilcoxon signed-rank test was performed. In same way, Friedman repeatedmeasures ANOVA on ranks was used instead of oneway repeated-measures ANOVA, when normality distribution was not verified. ANOVA was then followed by Tukey's post hoc test.

Validation of the strain gauge lickometer
Using licking as a behavioral readout requires measurement of the force applied on the spout with a high sensitivity. Although it has been shown that SG lickometers are able to detect licks when a mouse is drinking on a cup (Wang and Fowler, 1999), it has never been tested in a configuration where the SG is connected to a spout. Hence, before integrating our competitive SG lickometer, we validated its sensitivity by comparing the SG-offline and SG-online methods with the well established TS ( Fig.  2D; one-way repeated-measures ANOVA: n = 6, F = 4.906, p = 0.033). In doing so, we found that the sensitivity of the SG-offline method is comparable to TS (TS vs SG-offline, 0.685 6 0.0676 vs 0.740 6 0.0374, Tukey's test; TS vs SG-online, q = 33.073, p = 0.125) and significantly higher than SG-online (SG-online vs SG-offline: 0.546 6 0.0372 vs 0.740 6 0.0374, n = 6, Tukey's test; TS vs SG-online, q = 34.30, p = 0.031). In contrast, the TS and SG-online shared a similar sensitivity level (TS vs SG-offline: n = 6, Tukey's test, q = 31.227, p = 0.672). The PPV was not significatively different between the methods (Extended Data Fig. 2-1D; TS vs SG-offline vs SG-online; one-way repeated-measures ANOVA: n = 6, F = 0.166, p = 0.850).
We tested whether the sensitivity difference between the offline and online SG methods can be compensated for by lowering the value of the detection threshold. As illustrated in Figure 2B and Extended Data Figure 2-1A, the licks with a smaller amplitude did not cross the voltage threshold. By artificially adjusting the detection levels (Extended Data Fig. 2-1A,B), we showed that a reduction of only 20 mV is sufficient to improve the sensitivity greatly without compromising the PPV (Extended Data Fig. 2-1C,D). The potential to fine-tune the detection threshold greatly improves the real-time detection performance. We implemented an Arduino oscilloscope (Extended Data Fig. 2-1E, modified from https://create. arduino.cc/projecthub/aimukhin/advanced-oscilloscope-955395) to monitor the SG-signals and their thresholds online. Together, these data demonstrate that the SG lickometer can be successfully implemented to read out online licking behavior.

Behavioral training and serial RL
After having validated the ability of the SG lickometer to track individual licks, we programmed FreiBox to control an intrasession RL task (Fig. 3A,B). The results were obtained with a cohort of eight mice trained during 4-6 weeks to acquire the intrasession reversal. As illustrated in their individual (Fig. 3C, Extended Data Fig. 3-1A,B) and best (Fig. 3D) sessions, the mice were able to learn and reverse very quickly. Indeed, by using a learning criterion of 70% correct licking in a sliding average window of five trials, the mice succeeded in both DL and RL blocks in ,10 and 20 trials, respectively. On average (Extended Data Fig. 3-1B), the mice required more trials to succeed in RL than in DL, a feature that has been described earlier (Castañé et al., 2010). Based on those positive results, we refined the FreiBox capability to control multiple task variants via an easy-to-use Arduino-Python graphical user interface (GUI) interface (Extended Data Fig. 3-1C). With such improvements, we were able to perform serial RL (Extended Data Fig. 3-1D) or probabilistic RL (Parker et al., 2016) by simply adjusting the block number or reward probability, respectively.
Calcium imaging during discriminative learning DL paradigms have often been used under 2-photon conditions to explore the role of cortical areas in decisionmaking and motor execution (Chen et al., 2017;Guo et al., 2017;Galiñanes et al., 2018;Morrissette et al., 2019;Wu et al., 2020). To test the feasibility of coupling calcium imaging in FreiBox with such a behavioral task, we developed an active Miniscope commutator (Extended Data Fig. 4-1) and trained a mouse injected with the viral vector rAAV1-hSyn-jGCamP7f and implanted with a GRIN lens in OFC (Fig. 4A). Once the DL was complete, we performed recordings with the Miniscope (Fig. 4B,C). To synchronize calcium data with behavior, we recorded simultaneously with an Open Ephys system, the analog and TTL SG licking signals, the Miniscope frames TTL from the digital-to-analog converter (DAC) box as well as the synchronization signal from the FreiBox (Fig. 4C). Figure 4C shows an example of those signals recorded from two consecutive error trials (Errors #1 and #2) followed by a hit attempt (Hit). That trial sequence was the turning point of the session because afterward the hit performance continuously increased while the error rate stagnated (Fig. 4B).
To analyze calcium videos, we used the open-source tool library CaImAn (Giovannucci et al., 2019) to extract the spatial footprints (Fig. 4D) and their associated temporal calcium traces (Fig. 4E). After finding some calcium activities modulated during recorded behavior (see Materials and Methods), we aligned neuronal examples to behavioral events (Fig. 4E) and found that most of the modulated jGCamP7f-OFC-expressing neurons can be separated into three main populations, depending on their peak activity in the task periods, as follows: initiation (during WD), licking (during licking response), or postlick (during an RA period). We confirmed this response distribution by repeating the same experiment in two trained Thy1-GCaMP6f mice during serial RL, as illustrated for one of them in Extended Data Figures 3-1D and 4-2. These results illustrate the complex encoding of OFC neurons during goal-directed behavior and the ability of our task to dissect different neuronal processes.

Discussion
Since the emergence of head-restraint preparations, directional licking has been used as a behavioral readout to access numerous cognitive processes including short-term memory (Wang et al., 2021), decision-making (Wu et al., 2020), or motor preparation (Chen et al., 2017). Conventional approaches for monitoring online licking behavior use electrical and optical sensors (Weijnen, 1998;Williams et al., 2018) or high-speed video recordings combined with deep-learning approaches (Bollu et al., 2021;Catanese and Jaeger, 2021). While those lickometers are very efficient, the behavioral measurements are limited to the occurrence of the licks and cannot provide information about the force or vigor of these movements (Dudman and Krakauer, 2016). To overcome those limitations, we developed and validated a cost-effective and sensitive force lick sensor, which can be used with freely moving mice. In contrast to the available force lickometer which measures licking on a flat disk (Rudisch et al., 2022;Wang and Fowler, 1999), our SG lickometer can be easily integrated to measure directional licking in a large number of behavioral setups, including head-fixed settings. The SG lickometer gives the opportunity to correlate an unexplored aspect of licking behavior (i.e., the licking force) in both head-fixed and freely moving conditions, thereby allowing evaluation of movement vigor (Dudman and Krakauer, 2016) and measurement of the motivation of the animal to collect rewards (Deng et al., 2021).
Based on our novel FreiBox platform, we are able to combine freely moving licking-based behaviors (e.g., DL, RL, or set shifting) with the most recent optogenetic approaches (Table 1). As a proof of concept, we performed Miniscope calcium imaging in mice performing DL and RL. We recorded several response patterns in the OFC, demonstrating that our platform is able to capture complex neuronal dynamics reflecting several aspects of behavioral flexibility such as decision-making, motor planning, and action evaluation. We recorded calcium activity related to nose poking and licking, but also to a neuronal population expressing a long-lasting plateau following reward delivery. These findings corroborate results observed with electrophysiological striatal and cortical recordings of monkeys performing an RL task (Pasupathy and Miller, 2005;Histed et al., 2009). Altogether, these data introduce FreiBox as a useful tool to explore the neuronal substrates of behavioral flexibility underlying directional licking in rodents.
Adaptation of the methods to experimental questions is a major advantage of developing open-source tools (White et al., 2019). Our development of FreiBox and its low-cost parts represent complementary additions to the recently emerging battery of open-source techniques (Hou and Glover, 2022;White et al., 2019). Recently, open-source initiatives such as Miniscope (Aharoni et al., 2019) and Open Ephys (Siegle et al., 2017) have opened the gate to the standardization of flexible, lowcost, and open-source technologies. In a global context of budget reduction, open-source projects and publications constitute a great advance to reduce the financial dependency of the laboratories without compromising research quality.